package com.pinyou.service;

import com.jzfai.user.pojo.User;
import com.pinyou.auth.entity.UserInfo;
import com.pinyou.auth.utils.JwtUtils;
import com.pinyou.client.UserClient;
import com.pinyou.config.JwtProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Service;

/**
 * @Title: AuthService
 * @Description:
 * @Auther:jzfai
 * @Version: 1.0
 * @create 2020/3/14 16:56
 */
@Service
@EnableConfigurationProperties(JwtProperties.class)
public class AuthService {
    @Autowired
    private UserClient userClient;

    @Autowired
    private JwtProperties properties;
    public String authentication(String username, String password) {
        // 调用微服务，执行查询
        User user = this.userClient.queryUser(username, password);
        // 如果查询结果为null，则直接返回null
        if (user == null) {
            return null;
        }
        // 如果有查询结果，则生成token
        try {
            String token = JwtUtils.generateToken(new UserInfo(user.getId(), user.getUsername()),
                    properties.getPrivateKey(), properties.getExpire());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
